{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# <center>第1章 Python概述</center>\n",
    "\n",
    "<br>\n",
    "\n",
    "- [1.1 Python语言](#1.1-Python语言)\n",
    "  - [1.1.1 Python语言的发展](#1.1.1-Python语言的发展)\n",
    "    - [Python发展史上的重要版本](#Python发展史上的重要版本)\n",
    "  - [1.1.2 Python语言的特点](#1.1.2-Python语言的特点)\n",
    "    - [Python的缺点](#Python的缺点)\n",
    "  - [1.1.3 Python的主要应用领域](#1.1.3-Python的主要应用领域)\n",
    "- [1.2 Python 运行环境](#1.2-Python-运行环境)\n",
    "  - [1.2.1 Python 的不同实现和发行版](#1.2.1-Python-的不同实现和发行版)\n",
    "    - [Python的实现版本](#Python的实现版本)\n",
    "    - [Python发行版](#Python发行版)\n",
    "  - [1.2.2 Python 环境的安装](#1.2.2-Python-环境的安装)\n",
    "    - [官方标准版 Python 的安装](#官方标准版-Python-的安装)\n",
    "    - [Anaconda 发行版的安装](#Anaconda-发行版的安装)\n",
    "    - [安装第三方包](#安装第三方包)\n",
    "- [1.3 Python程序的运行](#1.3-Python程序的运行)\n",
    "  - [1.3.1 交互模式](#1.3.1-交互模式)\n",
    "    - [脚本模式](#脚本模式)\n",
    "- [1.4 常用开发工具](#1.4-常用开发工具)\n",
    "- [1.5 Python虚拟环境*](#1.5-Python虚拟环境*)\n",
    "  - [1.5.1 什么是虚拟环境](#1.5.1-什么是虚拟环境)\n",
    "  - [1.5.2 虚拟环境的创建和使用](#1.5.2-虚拟环境的创建和使用)\n",
    "    - [venv](#venv)\n",
    "    - [conda](#conda)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 1.1 Python语言\n",
    "### 1.1.1 Python语言的发展\n",
    "\n",
    "- 1989年，由荷兰人Guido Van Rossum开发\n",
    "- 版本\n",
    "  - 2.x\n",
    "  - 3.x\n",
    "  - 最重要的区别\n",
    "    - `print`函数 vs `print`语句\n",
    "    - `range` vs `range` and `xrange`\n",
    "    - `input` vs `input` and `raw_input`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Python发展史上的重要版本\n",
    "\n",
    "| 时间       | 版本          | 特点                                              |\n",
    "| ---------- | ------------- | ------------------------------------------------- |\n",
    "| 1991年2月  | Python 0.9    | 第一个正式版本，Python的雏形                      |\n",
    "| 1994年1月  | Python 1.0    | 增加了函数式编程的支持                            |\n",
    "| 2000年9月  | Python 2.0    | 实现了PEP（Python Enhancement Proposal）          |\n",
    "| 2008年10月 | Python 3.0    | 代码库更加简洁，改进了整数运算，增加了Unicode支持 |\n",
    "| 2010年7月  | Python 2.7    | 提供了一定程度与3.x的兼容性                       |\n",
    "| 2014年3月  | Python 3.4    | 采用`pip`作为Python包的默认安装方式               |\n",
    "| 2019年12月 | Python 3.8    | 引入大量新的语法，应用更加便捷、严谨              |\n",
    "| 2020年4月 | Python 2.7.18 | 最后一个2.7版本                                   |\n",
    "| 2020年8月 | Python 3.9|                                   |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 1.1.2 Python语言的特点\n",
    "\n",
    "- 代码简洁\n",
    "  - 更少的代码即能够完成相同的功能。代码的可读性强，对初学者非常友好。\n",
    "\n",
    "- 可扩展性好\n",
    "  - Python号称是一种胶水语言，能够通过多种扩展接口将其他编程语言的代码整合在一起。\n",
    "\n",
    "- 优异的跨平台性能\n",
    "  - Python具有优异的跨平台性能，代码可以不经过修改运行在任何安装了Python环境的计算机中\n",
    "\n",
    "- 多种编程方式\n",
    "  - 在Python中，可以使用面向过程、面向对象和函数式编程等编程方式编写代码\n",
    "\n",
    "- 丰富的工具包\n",
    "  - Python自身提供的数百个内置类和函数库，还可以很方便地从多种渠道安装第三方工具包，并利用它们快速构建程序\n",
    "\n",
    "- 开放源代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Python的缺点\n",
    "- 运行速度相对于编译型语言如C、C++、Java来说非常慢\n",
    "- 原因\n",
    "  - Python简洁易用的特点是拿运行速度换来的\n",
    "- 解决办法\n",
    "  - 利用高效的工具包\n",
    "  - 使用JIT编译技术提速\n",
    "  - 与其他语言混合编程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 1.1.3 Python的主要应用领域\n",
    "\n",
    "- Web开发\n",
    "\n",
    "- GUI程序开发\n",
    "\n",
    "- 网络爬虫\n",
    "\n",
    "- 数据分析\n",
    "\n",
    "- 人工智能\n",
    "\n",
    "- 自动化运维\n",
    "\n",
    "- 游戏开发"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 1.2 Python 运行环境\n",
    "\n",
    "### 1.2.1 Python 的不同实现和发行版\n",
    "\n",
    "#### Python的实现版本\n",
    "\n",
    "- CPython（https://www.python.org/downloads/）\n",
    "- IronPython：运行在.net环境中的Python实现\n",
    "- Jython：运行在Java虚拟机上的Python实现\n",
    "- PyPy：利用Python重新实现的Python，由于使用了JIT编译器，运行速度比CPython快的多"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Python发行版\n",
    "\n",
    "- ActivePython：包含了常见的数据科学和机器学习的环境\n",
    "  - https://www.activestate.com/activepython\n",
    "- python(x,y)：用于数值计算、数据分析和数据可视化的发行版，基于Qt图形用户界面和Spyder交互式开发环境\n",
    "  - http://python-xy.github.io\n",
    "- winpython：用于Windows系统，也是一个面各科学计算的发行版\n",
    "  - http://winpython.github.io\n",
    "- __Anaconda Python__：内置了包管理器conda以及常见的工具包，以及虚拟环境管理功能。由于使用便捷、功能齐全，近年来发展迅速，得到了很多用户的支持\n",
    "  - https://www.anaconda.com"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 1.2.2 Python 环境的安装\n",
    "\n",
    "#### 官方标准版 Python 的安装\n",
    "\n",
    "- 下载\n",
    "\n",
    "<center><img src=\"./figures/fig1-1.png\" width=\"60%\"/></center>\n",
    "\n",
    "\n",
    "- 运行Python安装程序\n",
    "\n",
    "<center><img src=\"./figures/fig1-2.png\" width=\"60%\"/></center>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Anaconda 发行版的安装\n",
    "\n",
    "- Anaconda是一种著名的Python发行版，内置了常用的科学计算工具包以及使用非常便捷的包管理工具conda。\n",
    "Anaconda本身是一种商业软件，但是其个人版本是免费的，此外conda包管理器也是开放源代码的。\n",
    "  - https://www.anaconda.com/products/individual#Downloads\n",
    "- 如果只需要使用conda包管理器来搭建自己的Python运行环境，可以选择Miniconda\n",
    "  - https://docs.conda.io/en/latest/miniconda.html\n",
    "\n",
    "<center><img src=\"./figures/fig1-4.png\" width=\"60%\"/></center>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### 安装第三方包\n",
    "\n",
    "- `pip`和`conda`和基本使用方法\n",
    "\n",
    "|功能|pip |conda|\n",
    "|:--|:--|:--|\n",
    "|安装 `pkg_name` 包 |`pip install pkg_name` |`conda install pkg_name`|\n",
    "|卸载 `pkg_name` 包| `pip uninstall pkg_name` |`conda uninstall pkg_name`|\n",
    "|列出已安装的包| `pip list` |`conda list`|\n",
    "|查找 `pkg_name` 包| `pip search pkg_name` |`conda search pkg_name`|\n",
    "|更新 `pkg_name` 包| `pip install pkg_name --upgrade` |`conda update pkg_name`|\n",
    "|更新全部包| — —| `conda update --all` |\n",
    "\n",
    "- 使用pip和conda安装包时还可以指定版本\n",
    "  - `pip install pkg_name==1.1.0`\n",
    "  - `conda install pkg_name==1.1.0`\n",
    "- 从源代码安装 Python 包\n",
    "  - 获取源代码并解压后，进入到源代码文件夹\n",
    "  - `python setup.py install`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 1.3 Python程序的运行\n",
    "\n",
    "### 1.3.1 交互模式\n",
    "\n",
    "- 启动交互环境\n",
    "\n",
    "<center><img src=\"./figures/fig1-5.png\" width=\"30%\"/></center>\n",
    "\n",
    "- 运行代码\n",
    "\n",
    "```bash\n",
    "$ python\n",
    "Python3.x.x(tags/v3.x.x:580fbb0, Jul202020,15:43:08)Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> print(\"Hello, Python!\")\n",
    "Hello Python!\n",
    ">>> for i in range(3):\n",
    "...     print(i)\n",
    "...\n",
    "0\n",
    "1\n",
    "2\n",
    ">>> exit()\n",
    "$\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### 脚本模式\n",
    "\n",
    "- 脚本文件 `hello.py`\n",
    "```python\n",
    "print (\"Hello, Python!\")\n",
    "```\n",
    "\n",
    "\n",
    "- 运行代码\n",
    "```bash\n",
    "$ python hello.py\n",
    "hello,world! \n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 1.4 常用开发工具\n",
    "\n",
    "- __IDLE__\n",
    "- __Spyder__\n",
    "- __IPython__\n",
    "- PyCharm\n",
    "- __Jupyter Notebook__\n",
    "- Eclipse with PyDev\n",
    "- Sublime Text\n",
    "- __Visual Studio Code__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 1.5 Python虚拟环境*\n",
    "\n",
    "### 1.5.1 什么是虚拟环境\n",
    "\n",
    "- 包之间存在着较为复杂的依赖关系，不同的项目会依赖相同的工具包的不同版本\n",
    "- 系统中安装的 Python 环境称为初始环境\n",
    "- 在初始环境中使用虚拟环境管理工具创建出来的独立的 Python 运行环境称为虚拟环境\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### 1.5.2 虚拟环境的创建和使用\n",
    "\n",
    "#### venv\n",
    "\n",
    "- 在当前目录下创建名为my_env的虚拟环境\n",
    "```bash\n",
    "$ python -m venv my_env\n",
    "```\n",
    "- 激活虚拟环境\n",
    "  - Windows\n",
    "  ```bash\n",
    "  my_env\\Scripts\\activate.bat\n",
    "  ```\n",
    "  - Linux & macOS\n",
    "  ```bash\n",
    "  source my_env/bin/activate\n",
    "  ```\n",
    "- 退出当前的虚拟环境\n",
    "  ```bash\n",
    "  deactivate\n",
    "  ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### conda\n",
    "- 创建虚拟环境my_env\n",
    "```bash\n",
    "conda create -n my_env python=3.8\n",
    "```\n",
    "- 激活虚拟环境\n",
    "```bash\n",
    "conda activate my_env\n",
    "```\n",
    "- 退出虚拟环境\n",
    "```bash\n",
    "conda deactivate\n",
    "```\n",
    "- 列出conda创建的全部虚拟环境\n",
    "```bash\n",
    "$ conda env list\n",
    "# conda environments:\n",
    "#\n",
    "base                  *  /path/to/anaconda\n",
    "my_env                   /path/to/anaconda/envs/my_env\n",
    "```\n",
    "- 删除虚拟环境\n",
    "```bash\n",
    "$ conda remove -n my_env --all\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "author": "liuchen",
  "celltoolbar": "幻灯片",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  },
  "rise": {
   "enable_chalkboard": true,
   "footer": "",
   "progress": true,
   "scroll": true,
   "slideNumber": true
  },
  "toc": {
   "base_numbering": "1",
   "nav_menu": {
    "height": "202.25543212890625px",
    "width": "191.99728393554688px"
   },
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "第1章 Python概述",
   "title_sidebar": "内容",
   "toc_cell": false,
   "toc_position": {
    "height": "602.6087036132812px",
    "left": "386px",
    "top": "187.44837951660156px",
    "width": "176.95652770996094px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  },
  "url": "https://github.com/hitlic/python_book"
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
